import MainLayout from "@/components/layout/MainLayout.vue";
import CommonLayout from "@/components/layout/CmmonLayout.vue";
import { createRouter, createWebHistory } from "vue-router";
import type { RouteRecordRaw } from "vue-router";

// 静态路由配置
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/",
    name: "Root",
    redirect: "/home",
    component: MainLayout,
    meta: { requiresAuth: true },
    children: [
      // 首页作为固定路由
      {
        path: "home",
        name: "Home",
        component: () => import("@/views/home/Index.vue"),
        meta: {
          title: "首页",
          icon: "House",
          showInSidebar: true,
          isTopLevel: true,
        },
      },
      {
        path: "user",
        name: "UserProfile",
        component: () => import("@/views/user/Index.vue"),
        meta: {
          title: "个人资料",
          icon: "User",
          showInSidebar: false,
        },
        children: [
          {
            path: "study-data",
            name: "StudyData",
            component: () => import("@/components/user/StudyData.vue"),
            meta: { title: "学习数据" },
          },
          {
            path: "study-plan",
            name: "StudyPlan",
            component: () => import("@/components/user/StudyPlan.vue"),
            meta: { title: "学习计划" },
          },
        ],
      },
    ],
  },
  {
    path: "/auth",
    component: CommonLayout,
    meta: { guestOnly: true, requiresAuth: false },
    children: [
      {
        path: "login",
        name: "Login",
        component: () => import("@/views/auth/Login.vue"),
      },
      {
        path: "register",
        name: "Register",
        component: () => import("@/views/auth/Register.vue"),
      },
    ],
  },
];

// 动态路由占位符
export const dynamicRoutes: RouteRecordRaw[] = [];

// 404 路由单独定义
export const notFoundRoute: RouteRecordRaw = {
  path: "/:pathMatch(.*)*",
  name: "NotFound",
  component: () => import("@/views/NotFound.vue"),
  meta: { title: "页面未找到" },
};

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [...constantRoutes], // 初始不包含404路由
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    }
    return { top: 0 };
  },
});

export default router;
